package cn.com.cym.gamezao.web.xmu;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.authorize.User;
import cn.com.cym.gamezao.entity.xmu.Recruit;
import cn.com.cym.gamezao.enums.UserType;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.service.recruit.CertificateService;
import cn.com.cym.gamezao.service.recruit.LearnExperienceService;
import cn.com.cym.gamezao.service.recruit.WorkExperienceService;
import cn.com.cym.gamezao.service.xmu.RecruitService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

@Api(tags = "招聘信息")
@RestController
@RequestMapping(value = "recruit")
@AllArgsConstructor
@AuthToken(rule = "xmu.recruit")
public class RecruitController {

    final RecruitService thisService;

    final CertificateService certificateService;

    final LearnExperienceService learnExperienceService;

    final WorkExperienceService workExperienceService;

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping("/page")
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        // 获取applicant用户
        //searchPage.setItems(new ArrayList<ConditionItem>().add(new ConditionItem().setName("").setItemType(ItemType.DICT).setValue("APPLICANT")));
        return ResponseEntity.ok(thisService.findByPageableAndUserType(searchPage, UserType.APPLICANT));
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping("admin/page")
    public ResponseEntity<?> selectPage(@RequestBody SearchPage searchPage){
        return ResponseEntity.ok(this.thisService.findByPageable(searchPage));
    }

    @ApiImplicitParam(name = "recruit", value = "recruit", required = true,dataType = "Recruit")
    @ApiOperation(value = "保存应聘患者信息！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping("/add")
    @AuthToken(rule = "add")
    public ResponseEntity<?> addOrModify(@RequestBody Recruit recruit){
        return ResponseEntity.ok(thisService.saveByCurrentUser(recruit));
    }
    //TODO 导出
    //@ApiImplicitParam(name = "recruit", value = "recruit", required = true,dataType = "Recruit")
    @ApiOperation(value = "获取当前用户的信息！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/info")
    @AuthToken(rule = "info")
    public ResponseEntity<?> info(){
        return ResponseEntity.ok(thisService.getByCurrentUser());
    }


    @ApiOperation(value = "获取当前用户的信息！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{id}")
    @AuthToken(rule = "search")
    public ResponseEntity<?> getById(@PathVariable Long id){
        return ResponseEntity.ok(thisService.getById(id));
    }

    @ApiOperation(value = "获取当前用户的信息！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{type}/{id}")
    @AuthToken(rule = "search")
    public ResponseEntity<?> getInfoById(@PathVariable  @NonNull Long id,@PathVariable @NonNull String type){
        final Recruit byId = this.thisService.getById(id);
        final User user = byId.getUser();
        if("cert".equals(type)){ // 获取证书管理
            return ResponseEntity.ok(certificateService.findAllByUser(user));
        }else if("learn".equals(type)){
            return ResponseEntity.ok(learnExperienceService.findAllByUser(user));
        }else if("work".equals(type)){
            return ResponseEntity.ok(workExperienceService.findAllByUser(user));
        }else {
            throw new BaseException("请输入正确的参数", new ResultResponse("请输入正确的参数", HttpStatus.BAD_REQUEST.value()));
        }
    }
}
